package com.wangyadong.hobby.javanetwork.udp;

import com.wangyadong.hobby.common.utils.BaseControllerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/7/169:58
 **/
public class UDPTest {
    private static Logger LOGGER = LoggerFactory.getLogger(UDPTest.class);

    public static void main(String[] args) throws InterruptedException {
//        serverUDP();
//        TimeUnit.SECONDS.sleep(1);
//        udptest1(args);
        UDPSixPart();
    }

    /**
     * UDP的请求
     * DatagramSocket 用来收发
     * DatagramPacket 用来发送数据包,可以接受的最大字节是8192
     *
     * @param args
     */
    public static void udptest1(String[] args) {
        try (DatagramSocket socket = new DatagramSocket(0)) {
            socket.setSoTimeout(5000);
            InetAddress host = InetAddress.getByName("localhost");
            DatagramPacket request = new DatagramPacket(new byte[1], 1, host, 13);
            //服务端
            byte[] data = new byte[1024];
            DatagramPacket response = new DatagramPacket(data, data.length);
            //发送数据包
            socket.send(request);
            //接受响应
            socket.receive(response);
            //接受返回数据
            String dayTime = new String(response.getData(), 0, response.getLength(), "US-ASCII");
            System.out.println("--->" + dayTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }

    /**
     * udp server
     */
    public static void serverUDP() {

        try (DatagramSocket socket = new DatagramSocket(13)) {
            while (true) {
                DatagramPacket request = new DatagramPacket(new byte[1024], 0, 1024);
                //接受请求
                socket.receive(request);
                String daytime = new Date().toString() + "\r\n" + "->wyd";
                byte[] bytes = daytime.getBytes("US-ASCII");
                InetAddress address = request.getAddress();
                int port = request.getPort();
                DatagramPacket resonpose = new DatagramPacket(bytes, bytes.length, address, port);
                socket.send(resonpose);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void UDPSixPart() {
        String str = "This is test.";
        byte[] bytes = new byte[0];
        try {
            bytes = str.getBytes("UTF-8");
            InetAddress byName = InetAddress.getByName("www.ibiblio.com");
            int port = 7;
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, byName, port);
            InetAddress address = dp.getAddress();
            byte[] data = dp.getData();
            int length = dp.getLength();
            int port1 = dp.getPort();
            int offset = dp.getOffset();
            SocketAddress socketAddress = dp.getSocketAddress();
            LOGGER.info("socketAddress [{}], address [{}] data:[{}] length:[{}] port1:[{}] offset:[{}]", socketAddress, address, data, length, port1, offset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    public static void setData() {

    }
}
